package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cjgl.entity.Student;
import cjgl.entity.Teacher;
import service.StudentService;
import service.TeacherService;

/**
 * Servlet implementation class PasswordServlet
 */
@WebServlet("/password")
public class PasswordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentService studentService=new StudentService();
	private TeacherService teacherService=new TeacherService();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if("studentPwd".contentEquals(method)) {
			studentPwd(request,response);
		}else if("teacherPwd".contentEquals(method)) {
			teacherPwd(request,response);
		}else if("saveStu".contentEquals(method)) {
			saveStu(request,response);
		}else if("saveTea".contentEquals(method)) {
			saveTea(request,response);
		}
	}
	private void saveTea(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		Teacher teacher = (Teacher) session.getAttribute("user");
		String yuan = request.getParameter("yuan");
		String xin = request.getParameter("xin");
		String xin2 = request.getParameter("xin2");
		if(yuan.equals(teacher.getPassword())) {
			 if(xin.equals(xin2)) {
				 teacher.setPassword(xin);
				teacherService.sava(teacher);
				session.removeAttribute("tea");
				response.sendRedirect("login.jsp");
			}else{
				request.setAttribute("cuowu", "两次密码输入不一致");
				request.getRequestDispatcher("password/teaPwd.jsp").forward(request, response);
			}
		}else {
			request.setAttribute("cuowu", "原密码输入错误");
			request.getRequestDispatcher("password/teaPwd.jsp").forward(request, response);
		}
		
	}
	private void saveStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student student = (Student) session.getAttribute("user");
		String yuan = request.getParameter("yuan");
		String xin = request.getParameter("xin");
		String xin2 = request.getParameter("xin2");
		if(yuan.equals(student.getPassword())) {
			 if(xin.equals(xin2)) {
				student.setPassword(xin);
				studentService.sava(student);
				session.removeAttribute("stu");
				response.sendRedirect("login.jsp");
			}else{
				request.setAttribute("cuowu", "两次密码输入不一致");
				request.getRequestDispatcher("password/stuPwd.jsp").forward(request, response);
			}
		}else {
			request.setAttribute("cuowu", "原密码输入错误");
			request.getRequestDispatcher("password/stuPwd.jsp").forward(request, response);
		}
	}
	private void teacherPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Teacher teacher = (Teacher) session.getAttribute("user");
		String teap = teacher.getPassword();
		request.setAttribute(teap, teap);
		request.getRequestDispatcher("password/teaPwd.jsp").forward(request, response);
	}
	private void studentPwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session=request.getSession();
		Student student= (Student) session.getAttribute("user");
		String password = student.getPassword();
		request.setAttribute(password, password);
		request.getRequestDispatcher("password/stuPwd.jsp").forward(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
